package com.hhsj.dynamicui.server.servlet.user;


import com.hhsj.androidserver.HttpServlet;
import com.hhsj.androidserver.HttpServletRequest;
import com.hhsj.androidserver.HttpServletResponse;
import com.hhsj.androidserver.WebServlet;
import com.hhsj.dynamicui.model.ADModel;
import com.hhsj.dynamicui.model.base.BaseModel;
import com.hhsj.dynamicui.server.AdJDBCService;
import com.hhsj.dynamicui.server.utils.ConstantUtil;

import org.json.JSONArray;
import org.json.JSONObject;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/uhomecp-app/advertising/advlist.json")
public class AdServlet extends HttpServlet {

    //
    // create table if not exists business_ad_list(
    // ad_id int(16) primary key auto_increment,
    // ad_type varchar(32) not null,
    // rank_no int(16) not null,
    // title varchar(64) not null,
    // title1 varchar(64),
    // image varchar(256) not null,
    // image1 varchar(256),
    // ad_desc varchar(32)
    // );
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse rep) throws Exception {
        this.doPost(req, rep);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse rep) throws Exception {
        System.out.println(HTTP_SERVLET + req.toString());
        System.out.println("\r\n");

        //new
        String param = req.getParameterValues("type")[0];
        String result = null;

        //查询数据
        String sql = String.format("SELECT * FROM business_ad_list where ad_type=\'%s\'", param);

        //insert into business_ad_list(ad_type,rank_no,title,image) values ('1',1,'标题1','https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1574341543240&di=3e74aa2fb915458047b084e457ba5fc9&imgtype=0&src=http%3A%2F%2Fbbs-fd.zol-img.com.cn%2Ft_s1200x5000%2Fg3%2FM03%2F0F%2F0A%2FCg-4V1QkzTmIUCgfAAvQ18E5JYQAAPb_gON7PEAC9Dv480.jpg');
        //insert into business_ad_list(ad_type,rank_no,title,image) values ('1',1,'标题2','https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1574341543240&di=3e74aa2fb915458047b084e457ba5fc9&imgtype=0&src=http%3A%2F%2Fbbs-fd.zol-img.com.cn%2Ft_s1200x5000%2Fg3%2FM03%2F0F%2F0A%2FCg-4V1QkzTmIUCgfAAvQ18E5JYQAAPb_gON7PEAC9Dv480.jpg');
        //insert into business_ad_list(ad_type,rank_no,title,image) values ('1',1,'标题3','https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1574341543240&di=3e74aa2fb915458047b084e457ba5fc9&imgtype=0&src=http%3A%2F%2Fbbs-fd.zol-img.com.cn%2Ft_s1200x5000%2Fg3%2FM03%2F0F%2F0A%2FCg-4V1QkzTmIUCgfAAvQ18E5JYQAAPb_gON7PEAC9Dv480.jpg');
        //insert into business_ad_list(ad_type,rank_no,title,image) values ('1',1,'标题4','https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1574341543240&di=3e74aa2fb915458047b084e457ba5fc9&imgtype=0&src=http%3A%2F%2Fbbs-fd.zol-img.com.cn%2Ft_s1200x5000%2Fg3%2FM03%2F0F%2F0A%2FCg-4V1QkzTmIUCgfAAvQ18E5JYQAAPb_gON7PEAC9Dv480.jpg');
        //insert into business_ad_list(ad_type,rank_no,title,image) values ('1',1,'标题5','https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1574341543240&di=3e74aa2fb915458047b084e457ba5fc9&imgtype=0&src=http%3A%2F%2Fbbs-fd.zol-img.com.cn%2Ft_s1200x5000%2Fg3%2FM03%2F0F%2F0A%2FCg-4V1QkzTmIUCgfAAvQ18E5JYQAAPb_gON7PEAC9Dv480.jpg');
        //insert into business_ad_list(ad_type,rank_no,title,image) values ('1',1,'标题6','https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1574341543240&di=3e74aa2fb915458047b084e457ba5fc9&imgtype=0&src=http%3A%2F%2Fbbs-fd.zol-img.com.cn%2Ft_s1200x5000%2Fg3%2FM03%2F0F%2F0A%2FCg-4V1QkzTmIUCgfAAvQ18E5JYQAAPb_gON7PEAC9Dv480.jpg');

        //insert into business_ad_list(ad_type,rank_no,title,image) values ('1',1,'标题1','https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1574341543240&di=3e74aa2fb915458047b084e457ba5fc9&imgtype=0&src=http%3A%2F%2Fbbs-fd.zol-img.com.cn%2Ft_s1200x5000%2Fg3%2FM03%2F0F%2F0A%2FCg-4V1QkzTmIUCgfAAvQ18E5JYQAAPb_gON7PEAC9Dv480.jpg');
        //insert into business_ad_list(ad_type,rank_no,title,image) values ('1',1,'标题2','https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1574341543240&di=3e74aa2fb915458047b084e457ba5fc9&imgtype=0&src=http%3A%2F%2Fbbs-fd.zol-img.com.cn%2Ft_s1200x5000%2Fg3%2FM03%2F0F%2F0A%2FCg-4V1QkzTmIUCgfAAvQ18E5JYQAAPb_gON7PEAC9Dv480.jpg');
        //insert into business_ad_list(ad_type,rank_no,title,image) values ('1',1,'标题3','https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1574341543240&di=3e74aa2fb915458047b084e457ba5fc9&imgtype=0&src=http%3A%2F%2Fbbs-fd.zol-img.com.cn%2Ft_s1200x5000%2Fg3%2FM03%2F0F%2F0A%2FCg-4V1QkzTmIUCgfAAvQ18E5JYQAAPb_gON7PEAC9Dv480.jpg');
        //insert into business_ad_list(ad_type,rank_no,title,image) values ('1',1,'标题4','https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1574341543240&di=3e74aa2fb915458047b084e457ba5fc9&imgtype=0&src=http%3A%2F%2Fbbs-fd.zol-img.com.cn%2Ft_s1200x5000%2Fg3%2FM03%2F0F%2F0A%2FCg-4V1QkzTmIUCgfAAvQ18E5JYQAAPb_gON7PEAC9Dv480.jpg');
        //insert into business_ad_list(ad_type,rank_no,title,image) values ('1',1,'标题5','https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1574341543240&di=3e74aa2fb915458047b084e457ba5fc9&imgtype=0&src=http%3A%2F%2Fbbs-fd.zol-img.com.cn%2Ft_s1200x5000%2Fg3%2FM03%2F0F%2F0A%2FCg-4V1QkzTmIUCgfAAvQ18E5JYQAAPb_gON7PEAC9Dv480.jpg');
        //insert into business_ad_list(ad_type,rank_no,title,image) values ('1',1,'标题6','https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1574341543240&di=3e74aa2fb915458047b084e457ba5fc9&imgtype=0&src=http%3A%2F%2Fbbs-fd.zol-img.com.cn%2Ft_s1200x5000%2Fg3%2FM03%2F0F%2F0A%2FCg-4V1QkzTmIUCgfAAvQ18E5JYQAAPb_gON7PEAC9Dv480.jpg');


        //insert into business_ad_list(ad_type,rank_no,title,image) values ('2',1,'标题1','https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1574341543240&di=3e74aa2fb915458047b084e457ba5fc9&imgtype=0&src=http%3A%2F%2Fbbs-fd.zol-img.com.cn%2Ft_s1200x5000%2Fg3%2FM03%2F0F%2F0A%2FCg-4V1QkzTmIUCgfAAvQ18E5JYQAAPb_gON7PEAC9Dv480.jpg');
        //insert into business_ad_list(ad_type,rank_no,title,image) values ('2',1,'标题2','https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1574341543240&di=3e74aa2fb915458047b084e457ba5fc9&imgtype=0&src=http%3A%2F%2Fbbs-fd.zol-img.com.cn%2Ft_s1200x5000%2Fg3%2FM03%2F0F%2F0A%2FCg-4V1QkzTmIUCgfAAvQ18E5JYQAAPb_gON7PEAC9Dv480.jpg');
        //insert into business_ad_list(ad_type,rank_no,title,image) values ('2',1,'标题3','https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1574341543240&di=3e74aa2fb915458047b084e457ba5fc9&imgtype=0&src=http%3A%2F%2Fbbs-fd.zol-img.com.cn%2Ft_s1200x5000%2Fg3%2FM03%2F0F%2F0A%2FCg-4V1QkzTmIUCgfAAvQ18E5JYQAAPb_gON7PEAC9Dv480.jpg');
        //insert into business_ad_list(ad_type,rank_no,title,image) values ('2',1,'标题4','https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1574341543240&di=3e74aa2fb915458047b084e457ba5fc9&imgtype=0&src=http%3A%2F%2Fbbs-fd.zol-img.com.cn%2Ft_s1200x5000%2Fg3%2FM03%2F0F%2F0A%2FCg-4V1QkzTmIUCgfAAvQ18E5JYQAAPb_gON7PEAC9Dv480.jpg');
        //insert into business_ad_list(ad_type,rank_no,title,image) values ('2',1,'标题5','https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1574341543240&di=3e74aa2fb915458047b084e457ba5fc9&imgtype=0&src=http%3A%2F%2Fbbs-fd.zol-img.com.cn%2Ft_s1200x5000%2Fg3%2FM03%2F0F%2F0A%2FCg-4V1QkzTmIUCgfAAvQ18E5JYQAAPb_gON7PEAC9Dv480.jpg');
        //insert into business_ad_list(ad_type,rank_no,title,image) values ('2',1,'标题6','https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1574341543240&di=3e74aa2fb915458047b084e457ba5fc9&imgtype=0&src=http%3A%2F%2Fbbs-fd.zol-img.com.cn%2Ft_s1200x5000%2Fg3%2FM03%2F0F%2F0A%2FCg-4V1QkzTmIUCgfAAvQ18E5JYQAAPb_gON7PEAC9Dv480.jpg');


        //insert into business_ad_list(ad_type,rank_no,title,image) values ('2',1,'标题1','https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1574341543240&di=3e74aa2fb915458047b084e457ba5fc9&imgtype=0&src=http%3A%2F%2Fbbs-fd.zol-img.com.cn%2Ft_s1200x5000%2Fg3%2FM03%2F0F%2F0A%2FCg-4V1QkzTmIUCgfAAvQ18E5JYQAAPb_gON7PEAC9Dv480.jpg');
        //insert into business_ad_list(ad_type,rank_no,title,image) values ('2',1,'标题2','https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1574341543240&di=3e74aa2fb915458047b084e457ba5fc9&imgtype=0&src=http%3A%2F%2Fbbs-fd.zol-img.com.cn%2Ft_s1200x5000%2Fg3%2FM03%2F0F%2F0A%2FCg-4V1QkzTmIUCgfAAvQ18E5JYQAAPb_gON7PEAC9Dv480.jpg');
        //insert into business_ad_list(ad_type,rank_no,title,image) values ('2',1,'标题3','https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1574341543240&di=3e74aa2fb915458047b084e457ba5fc9&imgtype=0&src=http%3A%2F%2Fbbs-fd.zol-img.com.cn%2Ft_s1200x5000%2Fg3%2FM03%2F0F%2F0A%2FCg-4V1QkzTmIUCgfAAvQ18E5JYQAAPb_gON7PEAC9Dv480.jpg');
        //insert into business_ad_list(ad_type,rank_no,title,image) values ('2',1,'标题4','https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1574341543240&di=3e74aa2fb915458047b084e457ba5fc9&imgtype=0&src=http%3A%2F%2Fbbs-fd.zol-img.com.cn%2Ft_s1200x5000%2Fg3%2FM03%2F0F%2F0A%2FCg-4V1QkzTmIUCgfAAvQ18E5JYQAAPb_gON7PEAC9Dv480.jpg');
        //insert into business_ad_list(ad_type,rank_no,title,image) values ('2',1,'标题5','https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1574341543240&di=3e74aa2fb915458047b084e457ba5fc9&imgtype=0&src=http%3A%2F%2Fbbs-fd.zol-img.com.cn%2Ft_s1200x5000%2Fg3%2FM03%2F0F%2F0A%2FCg-4V1QkzTmIUCgfAAvQ18E5JYQAAPb_gON7PEAC9Dv480.jpg');
        //insert into business_ad_list(ad_type,rank_no,title,image) values ('2',1,'标题6','https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1574341543240&di=3e74aa2fb915458047b084e457ba5fc9&imgtype=0&src=http%3A%2F%2Fbbs-fd.zol-img.com.cn%2Ft_s1200x5000%2Fg3%2FM03%2F0F%2F0A%2FCg-4V1QkzTmIUCgfAAvQ18E5JYQAAPb_gON7PEAC9Dv480.jpg');


        PreparedStatement statement = AdJDBCService.getInstance().getConnection().prepareStatement(sql);

        ResultSet resultSet = statement.executeQuery();

        List<ADModel> baseModels = new ArrayList<>();

        JSONArray array = new JSONArray();
        while (resultSet.next()) {
            String ad_id = String.valueOf(resultSet.getInt("ad_id"));
            String title = resultSet.getString("title");
            String title1 = resultSet.getString("title1");
            String image = resultSet.getString("image");
            String image1 = resultSet.getString("image1");
//            String action = resultSet.getString("action");
            String ad_desc = resultSet.getString("ad_desc");

            ADModel model = new ADModel(ad_id, title, title1, image, image1, null, ad_desc);
            baseModels.add(model);
            array.put(new JSONObject(model.toJSONString()));
        }


        BaseModel<ADModel> baseModel = new BaseModel<>();
        baseModel.setDataList(baseModels);
        baseModel.setCode("0");
        baseModel.setMsg("成功");

        if (ConstantUtil.DEBUG) {
            System.out.println(HTTP_SERVLET + baseModel.toJSONString());
        }

        rep.println(baseModel.toJSONString());
    }
}